/*
 * @Author: 小花
 * @Date: 2022-05-22 14:10:26
 * @LastEditors: 小花
 * @LastEditTime: 2022-05-28 18:17:50
 * @FilePath: /permission_client/src/model/MenuView.ts
 * @Description:
 */

import { IMenuType } from './MenuTypeView'
import IconList from '@/components/IconList.vue'
import { ElTable, FormInstance } from 'element-plus'

export interface IMenu {
  id: string,
  parentId: string,
  name: string,
  path: string,
  component: string,
  routerName: string,
  redirect: string,
  menuType: number,
  icon: string,
  isRoute: boolean,
  isLeaf: boolean,
  keepAlive: boolean,
  hidden: boolean,
  createBy: string,
  createTime: string,
  updateBy: string,
  updateTime: string,
  hasChildren?: boolean,
  children?: IMenu[],
}

export const isOptions = [
  { label: '是', value: true, type: '' },
  { label: '否', value: false, type: 'info' }
]

export const rules = {
  name: [
    { required: true, message: '请输入菜单的名称', trigger: 'change' }
  ],
  path: [
    { required: true, message: '请输入菜单地址', trigger: 'change' }
  ],
  parentId: [
    { required: true, message: '请选择父级菜单', trigger: 'change' }
  ],
  component: [
    { required: true, message: '请输入前端组件地址', trigger: 'change' }
  ]
}

export const EMenuType = {
  一级菜单: 0,
  子菜单: 1,
  按钮权限: 2
}

/**
 * @description: 获取当前菜单及所有子菜单
 * @param {IMenu} menu 菜单
 * @return {*}
 */
export const queryMenuAllAndChildren = (menu: IMenu[], result = [] as IMenu[]) => {
  menu.forEach(item => {
    result.push(item)
    if (item.children && item.children.length) {
      queryMenuAllAndChildren(item.children, result)
    }
  })
  return result
}

export default class MenuView {
  menuList = [] as IMenu[]
  dialogFormVisible = false
  iconList = {} as InstanceType<typeof IconList>
  shwoIconList = false
  form = {
    menuType: EMenuType.一级菜单,
    isRoute: true,
    keepAlive: false,
    hidden: false
  } as IMenu

  ruleFormRef = {} as FormInstance
  menuTypeList = [] as IMenuType[]
  multipleSelection = [] as IMenu[]
  multipleTableRef = {} as InstanceType<typeof ElTable>
}
